<?php
/**
 * --------------------------------------------
 * Desc：
 * User: Jiafang.Wang
 * Date: 2017/4/24 13:51
 * File: function.php
 * --------------------------------------------
 */
/**
 * @desc 时间转化
 * @param $the_time
 * @since 2017/4/24 13:54
 * @author Jiafang.Wang
 * @return string
 */
function time_tran($the_time)
{
    $now_time = date("Y-m-d H:i:s", time() + 8 * 60 * 60);
    $now_time = strtotime($now_time);
    $show_time = strtotime($the_time);
    $dur = $now_time - $show_time;
    if ($dur < 0) {
        return $the_time;
    } else {
        if ($dur < 60) {
            return $dur . '秒前';
        } else {
            if ($dur < 3600) {
                return floor($dur / 60) . '分钟前';
            } else {
                if ($dur < 86400) {
                    return floor($dur / 3600) . '小时前';
                } else {
                    if ($dur < 259200) {//3天内
                        return floor($dur / 86400) . '天前';
                    } else {
                        return date("Y-m-d", strtotime($the_time));
                    }
                }
            }
        }
    }
}

/**
 * http/https请求
 * @param $url
 * @param array $data
 * @param string $method
 * @param array $header
 * @param string $cookie
 * @return mixed
 */
function http_url($url, $data = array(), $method = 'post', $header = [], $cookie = '')
{
    $ch = curl_init();
    $method = strtolower($method);
    if($data && $method == 'get') {
        if(strpos($url, '?')) {
            $url .= '&';
        } else {
            $url .= '?';
        }
        if (is_array($data)){
            $url .= http_build_query($data);
        }else{
            $url .= $data;
        }
        $data = [];
    }
    curl_setopt($ch, CURLOPT_URL, $url);
    if (stripos($url, 'https://') !== false) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    if ($cookie) {
        if (is_array($cookie)) {
            $tmp = $cookie;
            $cookie = '';
            foreach ($tmp as $k => $v) {
                $cookie .= $k . '=' . $v . '; ';
            }
        }
        $header[] = 'Cookie: ' . $cookie;
    }
    if ($header) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }
    if ($data) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    $response = curl_exec($ch);

    curl_close($ch);
    return $response;
}

function toFloat($num){
    if (stripos($num, '%') !== false){
        return str_replace('%', '', $num)/100;
    }else{
        return str_replace(',', '', $num);
    }
}
//汉字 转 GBK的十六进制转换
function ascii_encode($str) {
    $str = mb_convert_encoding($str, 'GBK');
    $asc = '';
    for ($i = 0; $i < strlen($str); $i++) {
        $temp_str = dechex(ord($str[$i]));
        $asc .= "%".$temp_str[0].$temp_str[1];
    }
    return strtoupper($asc);
}

//判断是否属手机
function is_mobile(){
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    $mobile_agents = Array("240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipad","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte");
    $is_mobile = false;
    foreach ($mobile_agents as $device) {
        if (stristr($user_agent, $device)) {
            $is_mobile = true;
            break;
        }
    }
    return $is_mobile;
}

/**
 * @param $input    [需要修改的数组]
 * @param $offset   [插入的起始位置或键名后]
 * @param $length   [插入的长度或键名前]
 * @param $replacement  [需要插入的元素(array、string....)]
 * @return array
 */
function array_splice_assoc($input, $offset, $length=0, $replacement = array())
{
    $replacement = (array)$replacement;
    $key_indices = array_flip(array_keys($input));

    if (isset($input [$offset]) & is_string($offset)) {
        $offset = $key_indices [$offset];
    }
    if (isset($input[$length]) && is_string($length)) {
        $length = $key_indices [$length] - $offset;
    }

    // 先取出添加位置之前的元素与要添加的元素合并,再取添加位置之后的元素再合并
    $result= array_slice($input, 0, $offset, TRUE)
        + $replacement
        + array_slice($input, $offset + $length, NULL, TRUE);

    return $result;
}

/** 解析url，获取所需要的参数
 * @param $url
 * @param string $get
 * @return mixed
 */
function get_url_param($url, $get = ''){
    $param = parse_url($url);
    if (empty($param['query'])){
        return $url;
    }
    parse_str($param['query'], $query);
    if (empty($get)){
        return $query;
    }
    if (!isset($query[$get])){
        return null;
    }
    return $query[$get];
}
//获取联系日期
function getEveryDay($start = '', $end = ''){
    if (empty($start) || empty($end)){
        return [date('Y-m-d')];
    }
    $days = [];
    if (strpos($start, '-') !== false){
        $start = strtotime($start);
    }
    if (strpos($end, '-') !== false){
        $end = strtotime($end);
    }
    do{
        $days[] = date('Y-m-d', $start);
        $start = strtotime('+1 days', $start);
    }while($start <= $end);
    return $days;
}

//对比字符串差异
function getDiffString($str1, $str2,$encode='utf-8'){
    $diff = [];
    if (strcmp($str1, $str2) == 0){
        return $diff;
    }
    $num=mb_strlen($str2,$encode);
    if(strcmp($str1,$str2)==0)return array($str2,'');
    for($i=0;$i<$num;$i++){
        $word1=mb_substr($str1,$i,1,$encode);
        $word2=mb_substr($str2,$i,1,$encode);
        if($word1!=$word2){
            $diff[] = array(mb_substr($str2,0,$i,$encode),mb_substr($str2,$i,$num,$encode));
        };
    }
    return $diff;
}

//递归
function array_values_recursive($arr) {
    foreach ($arr as $key => $value)
    {
        if(is_array($value))
        {
            $arr[$key] = array_values($value);
            array_values_recursive($value);
        }
    }

    return $arr;
}

